/*
 * Created by C# Express.
 * User:Krug
 * Date: 2009-7-16
 * Time: 21:38
 * 
 * 
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using Toyger.SystemControl;
using System.Threading;
using Toyger.Common.Connections;

namespace Toyger.UI
{
	/// <summary>
	/// MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		Controller controller;
        String lastObject = String.Empty;
		public MainForm(Controller controller)
		{
			this.controller = controller;
			InitializeComponent();

            // Initialize Objectslist
            objectsList.Items.Clear();
            objectsList.Items.AddRange(controller.GetObjectList());
        }


        #region Menu and Button event handler
        private void connectTToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //controller.NewPage("Test");
        }
        #endregion


        internal void AddPage(TabPage tp)
        {
            tc.TabPages.Add(tp);
            tc.SelectedTab = tp;
            menuitem_Close.Enabled=true;
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tc.SelectedTab.Text.Equals("Home", StringComparison.OrdinalIgnoreCase)) return;
            TabPage tp = tc.SelectedTab;
            tc.TabPages.Remove(tp);
            tp.Dispose();
            while (tp.Disposing)
            {
                Thread.Sleep(100);
                Application.DoEvents();
            }
            if (tc.TabPages.Count==1)
            {
            	menuitem_Close.Enabled=false;
            }else{
            	tc.SelectedIndex=tc.TabPages.Count-1;
            }
        }

        private void objectsList_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                String obj = objectsList.SelectedItem.ToString();
                if (!lastObject.Equals(obj, StringComparison.OrdinalIgnoreCase))
                {

                    UserControl p = controller.GetSummuryPane(obj);
                    p.Dock = DockStyle.Fill;
                    gbObjectProperties.Text = obj;
                    gbObjectProperties.Controls.Clear();

                    gbObjectProperties.Controls.Add(p);
                    lastObject = obj;
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void btNew_Click(object sender, EventArgs e)
        {
            using (FormServerSelect fss = new FormServerSelect(controller.GetServerTypeList()))
            {
                if (fss.ShowDialog(this) == DialogResult.OK)
                {
                    if (!String.IsNullOrEmpty(fss.SelectedServerType))
                    {
                        controller.NewConnectionObject(fss.SelectedServerType);
                    }
                }
            }
        }




        internal void AddNewConnectionObject(String objectName)
        {
            objectsList.Items.Add(objectName);
            objectsList.SelectedItem = objectName;
        }
    }
}
